package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.DriverInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.DriverInfoRepository;
import co.yixiang.modules.shop.service.DriverInfoService;
import co.yixiang.modules.shop.service.dto.DriverInfoDto;
import co.yixiang.modules.shop.service.dto.DriverInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.DriverInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "driverInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DriverInfoServiceImpl implements DriverInfoService {

    private final DriverInfoRepository driverInfoRepository;

    private final DriverInfoMapper driverInfoMapper;

    public DriverInfoServiceImpl(DriverInfoRepository driverInfoRepository, DriverInfoMapper driverInfoMapper) {
        this.driverInfoRepository = driverInfoRepository;
        this.driverInfoMapper = driverInfoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(DriverInfoQueryCriteria criteria, Pageable pageable){
        Page<DriverInfo> page = driverInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(driverInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<DriverInfoDto> queryAll(DriverInfoQueryCriteria criteria){
        return driverInfoMapper.toDto(driverInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public DriverInfoDto findById(Integer id) {
        DriverInfo driverInfo = driverInfoRepository.findById(id).orElseGet(DriverInfo::new);
        ValidationUtil.isNull(driverInfo.getId(),"DriverInfo","id",id);
        return driverInfoMapper.toDto(driverInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public DriverInfoDto create(DriverInfo resources) {
        return driverInfoMapper.toDto(driverInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(DriverInfo resources) {
        DriverInfo driverInfo = driverInfoRepository.findById(resources.getId()).orElseGet(DriverInfo::new);
        ValidationUtil.isNull( driverInfo.getId(),"DriverInfo","id",resources.getId());
        driverInfo.copy(resources);
        driverInfoRepository.save(driverInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            driverInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DriverInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DriverInfoDto driverInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", driverInfo.getUserId());
            map.put("标题", driverInfo.getTitle());
            map.put("手机号", driverInfo.getPhone());
            map.put("年限", driverInfo.getYears());
            map.put("省", driverInfo.getProvince());
            map.put("市", driverInfo.getCity());
            map.put("区", driverInfo.getDistrict());
            map.put("薪资下限", driverInfo.getSalaryMin());
            map.put("薪资上限", driverInfo.getSalaryMax());
            map.put("具体内容", driverInfo.getMessage());
            map.put("图片", driverInfo.getImage());
            map.put("阅读次数", driverInfo.getCountNumber());
            map.put("创建时间", driverInfo.getCreateTime());
            map.put("是否删除", driverInfo.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}